package com.items.form.impl;

import net.mingsoft.cms.dao.TFormTableMapper;
import net.mingsoft.cms.dao.TFormValueMapper;
import net.mingsoft.cms.dto.BannerDto;
import net.mingsoft.cms.entity.Page;
import net.mingsoft.cms.entity.TFormTable;
import net.mingsoft.cms.entity.TFormValue;
import net.mingsoft.cms.service.ProductService;
import net.mingsoft.cms.service.base.DynamicServiceAbs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * 产品服务impl
 *
 * @author bai
 * @date 2023/02/08
 */
@Service
public class ProductServiceImpl extends DynamicServiceAbs<BannerDto> implements ProductService {

    @Resource
    private TFormTableMapper tFormTableMapper;

    @Resource
    private TFormValueMapper tFormValueMapper;

    @Autowired
    public ProductServiceImpl() {
        super("banner",BannerDto.class);
    }

    /**
     * 初始化动态表
     */
    @PostConstruct
    public void init(){
        List<TFormTable> formTables = tFormTableMapper.queryAllByLimit(new TFormTable(tableName), new Page.PageRequest(1, fieldLength,1));
        tFormTableMapper.insertOrUpdateBatch(getTableInit(formTables));
    }

    @Override
    public Boolean addFormList(BannerDto dto) {
        String rowId =  String.valueOf(tFormValueMapper.getUUid());
        try {
            return tFormValueMapper.insertBatch(getAddFormList(rowId, dto)) > 0;
        } catch (IllegalAccessException e){

        }
        return false;
    }


    @Override
    public List<BannerDto> getFormList(BannerDto dto) {
        try {
            List<TFormValue> list = tFormValueMapper.getFormAllRow(tableName, objToFormValue(dto));
            return getFormList(list);
        } catch (IllegalAccessException e){

        }
        return Collections.emptyList();
    }

    @Override
    public Boolean updateFormList(BannerDto dto){
        List<TFormValue> formRow =  tFormValueMapper.getFormRow(tableName, dto.getRowId());
        try {
             return tFormValueMapper.updateBatch(getUpdateFormList(formRow,dto)) > 0;
        } catch (IllegalAccessException e){
            //  xxx
        }
        return false;
    }


    @Override
    public Boolean delFormRow(String rowId) {
        return tFormValueMapper.delByRowId(tableName, rowId) > 0;
    }

}
